<?php
// 提交预约信息
// header("content-type:text/html;charset=utf-8");

include_once("conn.php");
include_once("u_getLoginInfo.php");
if($status != 1){
    die("<script>alert('账户处于待注销状态，不可操作！');history.go(-1);</script>");
}
$petID = $_POST['petID'];
$petName = $_POST['petName'];
$petType = $_POST['petType'];
$orderType = $_POST['orderType'];
$orderDate = $_POST['orderDate'];
$orderTime = $_POST['orderTime'];
$docID = $_POST['docID'];

if ($_POST['orderRemark'] == "") {
    $orderRemark = "无备注";
} else {
    $orderRemark = $_POST['orderRemark'];
}
$phone = $_POST['phone'];


$select_order_list =
    "SELECT * FROM order_infos 
     WHERE doc_ID = '" . $docID . "'
     AND orderDate = '" . $orderDate . "'";
$result_order_list = mysqli_query($connect_DB, $select_order_list);
if ($result_order_list) {
    $order_list_num = mysqli_num_rows($result_order_list);
    if ($order_list_num > 5) {
        die("<script>alert('您为" . $petName . "选择的医生剩余预约0位，请重新选择！');location='user_gotoOrder.php';</script>");
    } else {
        // 设置时区为中国时区
        date_default_timezone_set("Asia/Shanghai");
        // 获取当前时间戳
        $timestamp = time();
        // 获取当前年月日
        $nowdate = date("Y-m-d", $timestamp);
        $nowDate = new DateTime($nowdate);
        $getDate = new DateTime($orderDate);
        if (($nowDate->diff($getDate))->days > 7) {
            die("<script>alert('预约时间不能超过7天！请重新选择！');location='user_gotoOrder.php';</script>");
        }
        $nowdatestr = date("Ymd", $timestamp);
        // 获取当前时分秒
        $nowtime = date("H:i:s", $timestamp);
        // 生成一个五位随机数
        $random_number = substr((rand(10000, 99999)), 1);
        $order_ID = $nowdatestr . $random_number . substr($timestamp, -6);
        $insert_order_info =
            "INSERT INTO order_infos (orderID, pet_ID, user_ID, doc_ID, orderDate, orderTime, orderRemark, phone, subDate, subTime, orderType, orderState)
            VALUE ('" . $order_ID . "', '" . $petID . "', '" . $userID . "', '" . $docID . "', '" . $orderDate . "',
             '" . $orderTime . "', '" . $orderRemark . "', '" . $phone . "', '" . $nowdate . "', '" . $nowtime . "', '" . $orderType . "', 1)";
        $result_insert = mysqli_query($connect_DB, $insert_order_info);
        if ($result_insert) {
            echo "<script>alert('预约申请已提交！');location='user_noworder.php';</script>";
        } else {
            die("<script>alert('数据库写入失败，请稍后重试！');location='user_gotoOrder.php';</script>");
        }
    }
} else {
    die("<script>alert('传递预约申请失败，请重试！');location='user_gotoOrder.php';</script>");
}

$connect_DB->close();
?>